import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import arrToTree from '../utils/arrToTree'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    navbar: [],
    insertUserFlag: null,
    deletetUserFlag: null,
    updateUserFlag: null,
    insertRoleFlag: null,
    insertUserStatus: null,
    setUserRoleFlag: null,
    deleteRoleFlag: null,
    updateRoleFlag: null,
    setUserRoleQXFlag: null,
    insertMenuFlag: null,
    deleteMenuFlag: null,
    updateMenuFlag: null
  },
  mutations: {
    allMenus(state, nav) {
      state.navbar = nav
    },
    menuName(state, menuNames) {
      console.log(menuNames)
      state.insertUserFlag = false
      state.deletetUserFlag = false
      state.updateUserFlag = false
      state.insertRoleFlag = false
      state.insertUserStatus = false
      state.setUserRoleFlag = false
      state. deleteRoleFlag = false
      state.updateRoleFlag = false
      state.setUserRoleQXFlag = false
      state.insertMenuFlag = false
      state.deleteMenuFlag = false
      state.updateMenuFlag = false
      for (let i = 0; i < menuNames.length; i++) {
        if (menuNames[i] == '新增用户') {
          state.insertUserFlag = true
        } else if (menuNames[i] == '删除用户') {
          state.deletetUserFlag = true
        } else if (menuNames[i] == '修改用户') {
          state.updateUserFlag = true
        } else if (menuNames[i] == '新增角色') {
          state.insertRoleFlag = true
        } else if (menuNames[i] == '设置状态') {
          state.insertUserStatus = true
        } else if (menuNames[i] == '设置角色') {
          state.setUserRoleFlag = true
        } else if (menuNames[i] == '删除角色') {
          state.deleteRoleFlag = true
        } else if (menuNames[i] == '修改角色') {
          state.updateRoleFlag = true
        } else if (menuNames[i] == '设置角色权限') {
          state.setUserRoleQXFlag = true
        } else if (menuNames[i] == '新增菜单') {
          state.insertMenuFlag = true
        } else if (menuNames[i] == '删除菜单') {
          state.deleteMenuFlag = true
        } else if (menuNames[i] == '修改菜单') {
          state.updateMenuFlag = true
        }

      }
    }
  },
  actions: {
    findAllMenu(context) {
      axios.get("/menu/getAllMenu").then((res) => {

        const navMenus = arrToTree.arraytotree(res.data);
        // console.log(navMenus)
        context.commit('allMenus', navMenus)
      });
    },
    // 通过userLoginName 查询出所有该用户所具有的功能

    selectMenuByUserLoginName(context, userLoginName) {
      axios.get("/roleMenu/selectMenuByUserLoginName", { params: { user: userLoginName } })
        .then(res => {
          const m = res.data
          context.commit('menuName', m)
        })
    }
  },
  modules: {
  }
})
